package flc.ast.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivitySetPwdBinding;

public class SetPwdActivity extends BaseAc<ActivitySetPwdBinding> {

    private boolean isFirst = true;
    private String firstPassword = "", secondPassword = "";

    @Override
    protected int onCreate() {
        return R.layout.activity_set_pwd;
    }

    @Override
    protected void initView() {
        mDataBinding.ivSetPwdBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivCancel.setOnClickListener(this);
        mDataBinding.ivNext.setOnClickListener(this);

        mDataBinding.etPassword.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String str = s.toString();
                if (str.length() == 6) {
                    mDataBinding.ivNext.setImageResource(R.drawable.xyb2);
                } else {
                    mDataBinding.ivNext.setImageResource(R.drawable.xyb);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivCancel://取消
                if (isFirst) {
                    onBackPressed();
                } else {
                    mDataBinding.etPassword.setText("");
                }
                break;
            case R.id.ivNext:
                String str = mDataBinding.etPassword.getText().toString();
                if (str == null || str.length() < 6) return;
                if (isFirst) {
                    isFirst = false;
                    firstPassword = str;
                    mDataBinding.etPassword.setText("");
                    mDataBinding.ivCancel.setImageResource(R.drawable.dj_cxsr);
                } else {
                    secondPassword = str;
                    if (firstPassword.equals(secondPassword)) {
                        SetAnswerActivity.password = firstPassword;
                        startActivity(SetAnswerActivity.class);
                    } else {
                        ToastUtils.showShort(getString(R.string.password_is_different));
                    }
                }
                break;
        }
    }

    @Override
    protected void initData() {

    }
}